﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.DAL;
using Core.Utils;

namespace Core.BOL
{
    public class MyAlbumRepository : IDisposable
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public MyAlbumRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public void InsertMyAlbum(tSite_Photos_MyAlbum myAlbum)
        {
            context.tSite_Photos_MyAlbums.InsertOnSubmit(myAlbum);
        }

        public List<tSite_Photos_MyAlbum> GetImagesByUserID(Guid UID)
        {
            var query = (from ma in context.tSite_Photos_MyAlbums
                         join a in context.tSite_Photos_Albums on ma.AID equals a.ID
                         where ma.UID == UID
                         orderby ma.InsertDate descending
                         select ma).ToList();

            return query;
        }

        public tSite_Photos_MyAlbum GetRandImagesByUserID(Guid UID)
        {
            var query = (from ma in context.tSite_Photos_MyAlbums
                         join a in context.tSite_Photos_Albums on ma.AID equals a.ID
                         where ma.UID == UID
                         orderby Guid.NewGuid()
                         select ma).FirstOrDefault();

            return query;
        }

        public int GetCountImagesByUserID(Guid UID)
        {
            Int32 query = (from n in context.tSite_Photos_MyAlbums
                           where n.UID == UID
                           select n).Count();

            return query;
        }

        public bool IsInAlbum(int ImageID, Guid UserID)
        {
            int query = (from n in context.tSite_Photos_MyAlbums
                         where n.IID == ImageID && n.UID == UserID
                         select n).Count();
            return (query > 0);
        }

        public void DeleteUserImages(Guid UID)
        {
            List<tSite_Photos_MyAlbum> lImages = (from m in context.tSite_Photos_MyAlbums
                                                  where m.UID == UID
                                                  select m).ToList();

            if (lImages != null && lImages.Count > 0)
            {
                context.tSite_Photos_MyAlbums.DeleteAllOnSubmit(lImages);
            }
        }

        public void DeleteImagesByAlbumID(int AID)
        {
            List<tSite_Photos_MyAlbum> lImages = (from m in context.tSite_Photos_MyAlbums
                                                  where m.AID == AID
                                                  select m).ToList();

            if (lImages != null && lImages.Count > 0)
            {
                context.tSite_Photos_MyAlbums.DeleteAllOnSubmit(lImages);
            }
        }

        public void DeleteImagesByImageID(int IID)
        {
            List<tSite_Photos_MyAlbum> lImages = (from m in context.tSite_Photos_MyAlbums
                                                  where m.IID == IID
                                                  select m).ToList();

            if (lImages != null && lImages.Count > 0)
            {
                context.tSite_Photos_MyAlbums.DeleteAllOnSubmit(lImages);
            }
        }

        public void Save()
        {
            context.SubmitChanges();
        }

        public void Dispose()
        {
            context.Dispose();
        }
        #endregion
    }
}
